//
//  TestTableRows.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2022/1/17.
//  Copyright © 2022 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import CooperSwift
import RxSwift

class TestTableRowsVM: NSObject, TableViewModelProtocol, BindKeyProtocol, UITableViewDelegate, FormResultProtocol {
    
    let bag = DisposeBag()
    
    func initDown() {
        
        /// 分割线
        let lineRowConfig = lineRow(20)
        
        /// 单个按钮
        let btnRowsConfig = self.btnRow("单个按钮", cellHeight: 44, key: bindKey(bag), isTarget: true)
        
        /// 多个按钮
        let btnsControlConfig = [TableShowBtnConfig("多个按钮1"), TableShowBtnConfig("多个按钮2").then({
            $0.frame.top = 44
        })]
        let btnsRowsConfig = self.btnsRow(btnsControlConfig, cellHeight: 88)
        
        /// 单个文本
        let lbRowsConfig = self.lbRow("单个文本", lines: 0, padding: UIEdgeInsets(top: 8, left: 20, bottom: 8, right: 120))
        
        /// 单张图片
        let imageControlConfig = self.imageConfig("https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/320.jpg")
        let imageRowConfig = self.imageShowRow(imageControlConfig, key: bindKey(bag))
        
        /// 多个图片
        let cellWidth = (Screen.width - 16.0 - 24)/3.0
        var items = Array<CollectionCellConfigProtocol>()
        for i in 0...4 {
            let urlBTModel = TableShowBaseControlConfig().then {
                $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(i+320).jpg"
            }
            let urlModel = TestShowCollectImageCVCModel.init(urlBTModel).then {
                $0.cellSize = CGSize(cellWidth, cellWidth)
            }
            if i == 0 {
                urlModel.isLocationFirst = true
            }
            if i == 3 {
                urlModel.isLocationLast = true
            }
            items.append(urlModel)
        }
        /// collectionview 展示图片
        let cellHeight = cellWidth * CGFloat(Int(((items.count - 1) / 3 + 1))) + 8.0
        let imagesCollectionShowRowConfig = imagesShowByCollectionViewRow(items, cellHeight: cellHeight, key: bindKey(bag), isTarget: true)
        
        ///  多个view展示
        var imagesArray: [TableShowImageConfig] = []
        for index in 0...7 {
            let left: CGFloat = index % 3 == 0 ? 12 : 8
            let width = (Screen.width - 32.0 - 8)/3.0 - 0.01
            let config = imagesConfig("https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(320 + index).jpg", vLeftCons: left, vWidthCons: width)
            imagesArray.append(config)
        }
        let imagesMultImageViewRowConfig = imagesShowByMultImageViewRow(imagesArray, key: bindKey(bag), isTarget: true)
        
        /// 自定义 cell
        let testInputRowConfig = TestTableInputCellModel.init("")
        testInputRowConfig.dataKey = "dataKey"
        testInputRowConfig.showValue = "info"
        
        /// 自定义 collection
        let customCellWidth = (Screen.width - 16.0 - 24)/3.0
        var customItems = Array<CollectionCellConfigProtocol>()
        for i in 0...4 {
            let urlBTModel = TableShowBaseControlConfig().then {
                $0.url = "https://dss1.bdstatic.com/kvoZeXSm1A5BphGlnYG/skin_zoom/\(i+320).jpg"
            }
            let urlModel = TestTableCollectionCVCModel.init(urlBTModel).then {
                $0.cellSize = CGSize(customCellWidth, customCellWidth)
            }
            if i == 0 {
                urlModel.isLocationFirst = true
            }
            if i == 3 {
                urlModel.isLocationLast = true
            }
            customItems.append(urlModel)
        }
        /// collectionview 展示图片
        let customCellHeight = cellWidth * CGFloat(Int(((items.count - 1) / 3 + 1))) + 8.0
        let customCollectionShowRowConfig = imagesShowByCollectionViewRow(customItems, cellHeight: customCellHeight, key: bindKey(bag), isTarget: true)
        
        
        addFirstSectionItems([
            btnRowsConfig,
            btnsRowsConfig,
            lbRowsConfig,
            imageRowConfig,
            lineRowConfig,
            imagesCollectionShowRowConfig,
            lineRowConfig,
            imagesMultImageViewRowConfig,
            lineRowConfig,
            testInputRowConfig,
            lineRowConfig,
            customCollectionShowRowConfig,
            lineRowConfig,
        ])
    }
    
    @objc func btnTap(_ info: TableShowBtnConfig) {
        self.currentTV()?.reloadData()
        showWindowToast("单个按钮点击事件")
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return vm_tableView(tableView, heightForRowAt: indexPath)
    }
    
}
    
class TestTableRowsVC: UIViewController, TableEnableProtocol, BindKeyProtocol {
    
    let vm = ViewModelFactory.tableVM(TestTableRowsVM.self)
    let bag = DisposeBag()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configAll(vm, disposeBag: bag, edges: .zero)
    }
    
}
